package com.itheima.arrlist;

import com.sun.org.apache.xalan.internal.lib.ExsltSets;

import javax.print.attribute.standard.Sides;
import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.Scanner;

public class StudentSystem {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //创建一个集合容器
        ArrayList<Student1> list = new ArrayList<>();
        while (true) {
            //1.主界面菜单搭建
            System.out.println("------------- 学生系统 -------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看所有学生");
            System.out.println("5.退出!");
            System.out.println("请输入你的选择:");
            String s = sc.next();
            switch (s) {
                case "1":
                    System.out.println("请输入添加学生的信息!");
                    addStudent(list);
                    break;
                case "2":
                    System.out.println("请输入要删除的学生信息!学号或者姓名");
                    System.out.println("-----系统里已存在的信息------");
                    lookStudent(list);
                    System.out.println("-----请输入要删除的学号或者姓名-----");
                    removeStudent(list);
                    break;
                case "3":
//                    System.out.println("请输入要修改的学生信息");
                    setStudent(list);
                    break;
                case "4":

                    lookStudent(list);
                    break;
                case "5":
                    System.out.println("欢迎下次使用!");
                    return;

                default:
                    System.out.println("输入有误!请重新输入");
                    break;
            }
        }
    }

    //修改学生信息
    public static void setStudent(ArrayList<Student1> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学生信息,目前学生信息如下!");
        lookStudent(list);
        System.out.println("请输入要修改的学生的信息!");
        String num = sc.next();

        int index = getIndex(list, num);
      //判断一下.输入的索引存不存在.
        if (index == -1) {
            System.out.println("学号不存在");
        } else {
            System.out.println("查询正确,请修改学生学号");
            String sid = sc.next();
            System.out.println("修改学生名字");
            String name = sc.next();
            System.out.println("修改学生年龄");
            int age = sc.nextInt();
            System.out.println("修改学生生日");
            String birthday = sc.next();
            System.out.println("修改成功!");
            Student1 student1 = new Student1(sid, name, age, birthday);
            list.set(index, student1);
            System.out.println("修改后的学生信息如下:");
            lookStudent(list);
        }
//        for (int i = 0; i < list.size(); i++) {
//            Student1 s = list.get(i);
//            if (s.getSid().equals(num) || s.getName().equals(num)) {
//                System.out.println(s.getSid() + "   " + s.getName() + "   " + s.getAge() + "   " + s.getBirthday());
//                System.out.println("修改学生学号");
//                String sid = sc.next();
//                System.out.println("修改学生名字");
//                String name = sc.next();
//                System.out.println("修改学生年龄");
//                int age = sc.nextInt();
//                System.out.println("修改学生生日");
//                String birthday = sc.next();
//                //新建学生容器,把修改后的信息添加到学生容器中
//                Student1 student1 = new Student1(sid, name, age, birthday);
//                //修改集合中,该学生信息,
//                list.set(i, student1);
//                System.out.println("修改成功!修改后的学生信息如下: ");
//                System.out.println(student1.getSid() + "   " + student1.getName() + "   " + student1.getAge() + "   " + student1.getBirthday());
//
//            }
//    }

    }

    //删除学生
    public static void removeStudent(ArrayList<Student1> list) {
        Scanner sc = new Scanner(System.in);
        String num = sc.next();
        int index = getIndex(list, num);
        //判断删除的时候有没有这个索引,没有的话就让重新输入.有的话.根据索引查询下信息,然后删除
        if (index == -1) {
            System.out.println("学生信息不存在,请重新输入!");
        } else {
            list.remove(index);
            System.out.println("删除成功!现在的学生信息");
            //删除成功后.调用一下查看学生信息.看看目前删了没有.
            lookStudent(list);
        }
    }


    //查看所有学生
    public static void lookStudent(ArrayList<Student1> list) {
        // 先判断一下集合中是否有信息.list.size()集合个数如果为0.就说明里面没有元素存在!
        if (list.size() == 0) {
            System.out.println("没有学生信息,请添加后再次查询!");
        }

        System.out.println("学号\t姓名\t年龄\t\t生日");
        for (int i = 0; i < list.size(); i++) {
            Student1 stu = list.get(i);
            System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getBirthday());
        }
    }

    //添加学生方法
    public static void addStudent(ArrayList<Student1> list) {
        Scanner sc = new Scanner(System.in);
        String sid;
        while (true) {
            System.out.println("请输入学号!");
            sid = sc.next();
            int index = getIndex(list, sid);
            // index== -1.说明不存在,可以输入.跳出循环.
            if (index == -1) {
                //多会添加的学号不存在.多会跳出循环.
                break;
            } else {
                //说明index==0 代表有索引了.说明传入的sid存在了.
                System.out.println("学号已存在,请重新输入");
            }
        }

        System.out.println("学生姓名");
        String name = sc.next();
        System.out.println("学生年龄");
        int age = sc.nextInt();
        System.out.println("学生生日");
        String birthday = sc.next();
        //输入的数据添加到学生集合
        Student1 student1 = new Student1(sid, name, age, birthday);
        list.add(student1);
        System.out.println("添加成功!");
    }


    //学号是否存在方法
    public static int getIndex(ArrayList<Student1> list, String sid) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student1 stu = list.get(i);
            String s = stu.getSid();
            if (s.equals(sid)) {
                index = i;
            }

        }

        return index;
    }
}